<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/21
 * Time: 11:17
 */

namespace App\Services;

use App\Goods;
use App\GoodsInquirys;
use App\GoodsParams;
use App\GoodsSpecs;
use DB;
use App\GoodsCategory;
use App\Lib\Util\QueryPager;

class GoodsService
{
    public function getGoods(Array $input, $paging = true)
    {
        $query = new Goods();

        if (!empty($input['goods_name'])) {
            $query = $query->where('goods_name', 'like', '%' . $input['goods_name'] . '%');
        }

        if (!empty($input['goods_vendor'])) {
            $query = $query->where('goods_vendor', 'like', '%' . $input['goods_vendor'] . '%');
        }

        if (!empty($input['list_flag'])) {
            $query = $query->where('list_flag', $input['list_flag']);
        }

        if (!empty($input['goods_category'])) {
            $service = new GoodsCategoryService();
            $list    = $service->getAllCate();
            $child   = $service->getAllChild($list, $input['goods_category']);
            $child[] = $input['goods_category'];
            $query   = $query->whereHas('goodsCategory', function ($query) use ($child) {
                $query->whereIn('goods_categories.id', $child);
            });
        }

        if (!empty($input['goods_score_min']) || !empty($input['goods_score_max'])) {
            if (!empty($input['goods_score_min']) && !empty($input['goods_score_max'])) {
                if ($input['goods_score_min'] > $input['goods_score_max']) {
                    $mid                      = $input['goods_score_max'];
                    $input['goods_score_max'] = $input['goods_score_min'];
                    $input['goods_score_min'] = $mid;
                }
            }
            $query = $query->whereBetween('goods_score', [$input['goods_score_min'] ?: Goods::$GOODS_SCORE_MIN, $input['goods_score_max'] ?: Goods::$GOODS_SCORE_MAX]);
        }

        $pager = new QueryPager($query);

        $pager->mapField('list_flag', Goods::$GOODS_LIST_FLAG_MAP);

        $pager->setRefectionMethodField('goodsCategoryGet');
        $pager->setRefectionMethodField('getFullPicturePath');

        return $paging ? $pager->doPaginate($input, 'sort_order') :
            $pager->queryWithoutPaginate($input, 'sort_order');
    }


    public function goodsAdd(Array $input)
    {
        Goods::create([
            'goods_name'        => $input['goods_name'],
            'goods_vendor'      => $input['goods_vendor'],
            'goods_score'       => $input['goods_score'],
            'sort_order'        => $input['sort_order'],
            'goods_category_id' => $input['goods_category_id'],
            'goods_picture'     => $input['goods_picture'],
            'list_flag'         => $input['list_flag'],
            'goods_content'     => $input['goods_content'],
        ]);
    }

    public function goodsEdit(Array $input)
    {
        Goods::where('id', $input['id'])->update([
            'goods_name'        => $input['goods_name'],
            'goods_vendor'      => $input['goods_vendor'],
            'goods_score'       => $input['goods_score'],
            'sort_order'        => $input['sort_order'],
            'goods_category_id' => $input['goods_category_id'],
            'goods_picture'     => $input['goods_picture'],
            'list_flag'         => $input['list_flag'],
            'goods_content'     => $input['goods_content'],
        ]);
    }

    public function goodsDel(Array $input)
    {
        DB::transaction(function () use ($input) {
            GoodsParams::where('goods_id', $input['id'])->delete();
            GoodsSpecs::where('goods_id', $input['id'])->delete();
            Goods::where('id', $input['id'])->delete();
        });
    }

    public function getGoodsInquirys(Array $input, $paging = true)
    {
        $query = new GoodsInquirys();

        if(!empty($input['goods_name'])){
            $query = $query->whereHas('goods',function($query)use ($input){
                $query->where('goods.goods_name','like','%'.$input['goods_name'].'%');
            });
        }

        if(!empty($input['goods_vendor'])){
            $query = $query->whereHas('goods',function($query)use ($input){
                $query->where('goods.goods_vendor','like','%'.$input['goods_name'].'%');
            });
        }

        if(!empty($input['display_flag'])){
            $query = $query->where('display_flag',$input['display_flag']);
        }

        if(!empty($input['goods_category'])){
            $service = new GoodsCategoryService();
            $list    = $service->getAllCate();
            $child   = $service->getAllChild($list, $input['goods_category']);
            $child[] = $input['goods_category'];
            $query = $query->whereHas('goods',function($query)use($child){
               $query->whereIn('goods.goods_category_id',$child);
            });
        }

        if(!empty($input['process_status'])){
            $query = $query->where('process_status',$input['process_status']);
        }
        $pager = new QueryPager($query);

        $pager->mapField('display_flag', GoodsInquirys::$DISPLAY_FLAG_MAP);

        $pager->setRefectionMethodField('goodsGet');
        $pager->setRefectionMethodField('goodsCategoryGet');
        $pager->setRefectionMethodField('usersGet');

        return $paging ? $pager->doPaginate($input, 'id') :
            $pager->queryWithoutPaginate($input, 'id');
    }

    public function changeInquirysShow($id)
    {
        $info = GoodsInquirys::where('id', $id)->first();
        if ($info->display_flag == GoodsInquirys::$DISPLAY_FLAG_ON) {
            $displayFlag = GoodsInquirys::$DISPLAY_FLAG_OFF;
        } else if ($info->display_flag == GoodsInquirys::$DISPLAY_FLAG_OFF) {
            $displayFlag = GoodsInquirys::$DISPLAY_FLAG_ON;
        }
        GoodsInquirys::where('id', $id)->update([
            'display_flag' => $displayFlag
        ]);
    }

    public function changeProcess($id)
    {
        GoodsInquirys::where('id', $id)->update([
            'process_status' => GoodsInquirys::$PROCESS_STATUS_FINISH
        ]);
    }

}